﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using XendApp;

namespace XendAppTest
{
    public partial class frmXendAppTest : Form
    {
        public frmXendAppTest()
        {
            InitializeComponent();
        }

        private Dictionary<Guid, List<Channel>> dicProviders = new Dictionary<Guid, List<Channel>>();

        private void Form1_Load(object sender, EventArgs e)
        {
            cboMessageType.SelectedIndex = 0;
            dteExpire.Value = DateTime.Now.AddDays(30);
            
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                dicProviders.Clear();
                cboProvider.Items.Clear();
                cboChannel.Items.Clear();
                Client c = new Client(txtUserName.Text, txtPassword.Text,null);
                if (rdoProviderChannelSubscribing.Checked)
                {
                    foreach (Provider p in c.GetSubcribedProviderChannelByUser())
                    {
                        dicProviders.Add(p.Id, p.Channels);
                        cboProvider.Items.Add(p);
                    }
                }
                else if (rdoProviderChannelCreated.Checked)
                {
                    foreach (Provider p in c.GetCreatedProviderChannelByUser())
                    {
                        dicProviders.Add(p.Id, p.Channels);
                        cboProvider.Items.Add(p);
                    }
                }

                if (cboProvider.Items.Count > 0)
                {
                    cboProvider.SelectedIndex = 0;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            Cursor = Cursors.Default;
        }

        private void cboProvider_SelectedValueChanged(object sender, EventArgs e)
        {
            RefreshChannels();
        }

        private void RefreshChannels()
        {
            Cursor = Cursors.WaitCursor;
            cboChannel.Items.Clear();
            if (cboProvider.SelectedItem != null)
            {
                Provider provider = (Provider)cboProvider.SelectedItem;

                foreach (Channel channel in dicProviders[provider.Id])
                {
                    cboChannel.Items.Add(channel);
                }
            }

            if (cboChannel.Items.Count > 0)
            {
                cboChannel.SelectedIndex = 0;
            }
            Cursor = Cursors.Default;
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                if (cboChannel.SelectedItem == null)
                {
                    MessageBox.Show("No Channel is selected.", "Select Channel", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                // create new message object
                XendApp.Message m = new XendApp.Message();

                // channel
                Channel channel = (Channel)cboChannel.SelectedItem;
                m.ChannelId = channel.Id;

                // subject
                m.Subject = txtSubject.Text;

                // body
                m.Body = txtBody.Text;

                // expiry date of message
                if (rdoExpireDate.Checked)
                {
                    m.ExpireDays = Convert.ToInt32(numExpireDays.Value);
                }
                else if (rdoExpireDays.Checked)
                {
                    m.SetExpireDate(dteExpire.Value);
                }

                // message type
                switch (cboMessageType.Text)
                {
                    case "Plain text":
                        m.ContentType = XendApp.Message.ContentT.PlainText;
                        break;
                    case "HTML":
                        m.ContentType = XendApp.Message.ContentT.HTML;
                        break;
                }

                // encryption
                m.Encrypted = chkEncrypted.Checked;

                // create client for sending
                Client c = new Client(txtUserName.Text, txtPassword.Text,null);

                // send message
                SendResult sr = c.Send(m);
                if (sr.Success)
                {
                    MessageBox.Show("Send was successful. Message Id is: " + sr.Id.ToString());
                }
                else
                {
                    MessageBox.Show("Send faild. Error: " + sr.ErrorMessage);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unhandled error: " + ex.Message);
            }


        }
    }
}
